<template>
    <el-form :model="form" :rules="rules" ref="formRef" label-width="100px">
        <el-form-item label="留言" prop="leaveWord">
            <el-input v-model="form.leaveWord" placeholder="请输入留言" />
        </el-form-item>

        <el-form-item label="回复" prop="answer">
            <el-input v-model="form.answer" placeholder="请输入回复" />
        </el-form-item>

        <el-form-item label="咨询师ID" prop="consultantId">
            <el-input-number v-model="form.consultantId" :min="1" placeholder="请输入咨询师ID" />
        </el-form-item>

        <!-- <el-form-item label="删除" prop="del">
            <el-switch v-model="form.del" active-value="1" inactive-value="0" active-text="已删除" inactive-text="未删除" />
        </el-form-item> -->

        <el-form-item>
            <el-button type="primary" @click="submitForm">提交</el-button>
            <el-button @click="resetForm">重置</el-button>
        </el-form-item>
    </el-form>
</template>

<script setup>
import { ref } from 'vue';
import { ElMessage } from 'element-plus';
import axios from 'axios';

// 表单数据
const form = ref({
    leaveWord: '',
    answer: '',
    consultantId: null,
    del: 0
});

// 表单验证规则
const rules = {
    leaveWord: [{ required: true, message: '请输入留言', trigger: 'blur' }],
    answer: [{ required: false, message: '请输入回复', trigger: 'blur' }],
    consultantId: [{ required: false, type: 'number', message: '请输入有效的咨询师ID', trigger: 'blur' }]
};

const formRef = ref(null);

// 提交表单
const submitForm = () => {
    formRef.value.validate(async (valid) => {
        if (valid) {
            await axios.post("/api/answerLeaveWord/add", form.value);
            ElMessage.success('提交成功！');
            console.log('表单数据：', form.value);
        } else {
            ElMessage.error('请检查输入内容');
        }
    });
};

// 重置表单
const resetForm = () => {
    formRef.value.resetFields();
};
</script>

<style scoped>
.el-form {
    max-width: 500px;
    margin: 0 auto;
}
</style>
